package xyz.xtt.asset.vo;

import java.util.Date;
import java.util.Objects;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.request.OperateUpRequest;
import xyz.xtt.asset.utils.DateUtils;

@Slf4j
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PreSellInfoVo {
	private Long preStart;
	private Long preEnd;

	/**
	 * @param operateUpRequest
	 * @param secodeType
	 * @param checkPublicityPeriod 公示期是否完整
	 * @return
	 */
	public static PreSellInfoVo buildCalc(OperateUpRequest operateUpRequest, int secodeType, boolean checkPublicityPeriodEnd) {
		int offset = 15;
		if (Objects.equals(secodeType, 3)) {
			Date date = DateUtil.date();
			Date beginOfDay = DateUtil.beginOfDay(date);
			Long emptyStart = DateUtil.toInstant(beginOfDay).toEpochMilli();
			return PreSellInfoVo.builder().preStart(emptyStart).preEnd(emptyStart).build();
		}
		// 没有预售期 等于当前时间

		boolean checkPresellIsNull = operateUpRequest.checkPresellIsNull();
		long curr = System.currentTimeMillis();
		Long start = curr;
		Long end = curr;
		if (checkPresellIsNull) {
			Date date = DateUtil.date();
			Date beginOfDay = DateUtil.beginOfDay(date);
			// 如果完整公示, 没填时间，算到当天开始
			// 没有完整公示，向后退15天，前14天为公示时间
			if (checkPublicityPeriodEnd) {
				Long beginTs = DateUtil.toInstant(beginOfDay).toEpochMilli();
				return PreSellInfoVo.builder().preStart(beginTs).preEnd(beginTs).build();
			}

			Date newDate = DateUtil.offset(beginOfDay, DateField.DAY_OF_MONTH, offset);
			Long emptyStart = DateUtil.toInstant(newDate).toEpochMilli();
			start = end = emptyStart;
		} else {
			start = DateUtils.strToEpochMilli(operateUpRequest.getPresellStart(), " 00:00:00");
			end = DateUtils.strToEpochMilli(operateUpRequest.getPresellEnd(), " 23:59:59");
		}
		return PreSellInfoVo.builder().preStart(start).preEnd(end).build();
	}

	public static PreSellInfoVo buildDefault() {
		return PreSellInfoVo.builder().preStart(0L).preEnd(0L).build();
	}

	public void showPublic() {
		String start = DateUtil.format(DateUtil.date(preStart), DatePattern.NORM_DATETIME_PATTERN);
		String send = DateUtil.format(DateUtil.date(preEnd), DatePattern.NORM_DATETIME_PATTERN);
		log.info("s1:{}", start);
		log.info("s2:{}", send);
	}

}
